/* Form */
function common_add(title,url){
	$('#common-dialog').dialog('open').dialog('setTitle', title); 
    $('#common-form').attr('action',url);
}

function common_edit(title,url){
	$('#common-dialog').show();
    var row = $('#common-table').datagrid('getSelected');
    if (row == null) {
		$.messager.alert('提示', '请先选择,再进行编辑！','info');
		return;
	}
    $('#common-dialog').dialog('open').dialog('setTitle', title);
    $('#common-form').form('load',row);
    $('#common-form').attr('action',url);
}

function common_delete(url,failcode,fail_info,typecode){
	var nodes = $('#common-table').datagrid('getSelections');
	if ($.isEmptyObject(nodes)) {
		$.messager.alert('提示', '请先选择,再进行删除 ！','info');
		return;
	}
	$.messager.confirm('确认', '确定要删除所选数据？', function (r) {
		if (r) {
			var ids='';
			for(var i=0;i<nodes.length;i++ ){
				ids+=','+nodes[i].id;
			}
			$.ajax({
				url : url,
				type : 'post',
				dataType : 'json',
				data : {
					ids : ids.substring(1)
				},
				success : function(data) {
					if(data == 0){
						$.messager.alert('提示', '操作成功！', 'info');
						if(1==typecode)
							$('#common-table').treegrid('reload');
						else
		   	    			$('#common-table').datagrid('reload');
					}else if(data == failcode)
						$.messager.alert('提示', fail_info, 'info');
					else if(data==1){
						$.messager.alert('提示','只能删下级代理！','info');
					}else if(data==202){
						$.messager.alert('提示','含有数据,无法删除！','info');
					}
					else
						$.messager.alert('提示', '处理失败，请请联系管理员！', 'info');
				},
			});
		 }
	})
}

function common_submit(failcode,fail_info,typecode){
	$('#common-form').form('submit',{
    	onSubmit:function(){
   	    	$(".panel-tool-close").trigger("click");
            return $(this).form('enableValidation').form('validate');
        },
	    success:function(data){
    		$('#common-form').form('clear');
   	    	if(data == 0){
				$.messager.alert('提示', '操作成功！', 'info');
				if(1==typecode)
					$('#common-table').treegrid('reload');
				else
   	    			$('#common-table').datagrid('reload');
   	    	}else if(data == failcode)
				$.messager.alert('提示', fail_info, 'info');
			else
				$.messager.alert('提示', '处理失败，请联系管理员！', 'info');
	    }
    });
}

function common_reset(){
	var diatitle=$('#common-dialog').panel('options').title;
	if (diatitle.indexOf('编辑') > 0) {
		var row = $('#common-table').datagrid('getSelected');
		$('#common-form').form('load',row);
	}else{
	    $('#common-form').form('clear');
	}
}

// 关闭弹出层
function closediv(){
	$(".panel-tool-close").trigger("click");
}
 
/**	条件查询 - 点击展示或隐藏	*/
function doSearch(){
	if($('#searchTerm').is(":visible")){
		$("#searchTerm").hide();
		$('#showItem').html('<i class="fa fa-plus-square-o fa-lg"></i>&nbsp;<font size="15">查询</font>');
	}else{
		$("#searchTerm").show();
		$('#showItem').html('<i class="fa fa-minus-square-o fa-lg"></i>&nbsp;<font size="15">隐藏</font>');
	}
}

function fmtimg(value,row,index){
	//return "<a target='_blank' href ='"+value+"'><img style='width:80px;height:30px;' border='1' src='"+value+"'/></a>";
	return "<a target='_blank' href ='"+value+"'>"+value+"</a>";
}

function formatDate(value,row,index){
	var all,year,month,day,hour,minute,second='';
	if(value != null && value != undefined && value != 0 && value != ""){
		value = ''+value;
		if(value.length > 14) 
			return value;
		year = value.substr(0,4);
		month = value.substr(4,2);
		if(value.length <8)
			return year+'-'+month;
		day = value.substr(6,2);
		if(value.length <11)
			return year+'-'+month+'-'+day;
		hour = value.substr(8,2);
		minute = value.substr(10,2);
		second = value.substr(12,2);
		return year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
	}
	return null;
}

function myformatter(date){
	if(date==null || date == '')
		return null;
    var y = date.getFullYear();
    var m = date.getMonth()+1;
    var d = date.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
}


function myparser2(s){
	var y,m,d = '';
	if(s != null && s != undefined && s != 0 && s != ""){
		s = ''+s;
		if(s.length <10)
			return '';
	}
    if(s.indexOf("-") > 0 ){
	    var ss = s.split('-');
	    y = parseInt(ss[0],10);
	    m = parseInt(ss[1],10);
	    d = parseInt(ss[2],10);
    }else{
	    var yStr = s.substr(0,4);
		var mStr = s.substr(4,2);
		var dStr = s.substr(6,2);
	    y = parseInt(yStr,10);
	    m = parseInt(mStr,10);
	    d = parseInt(dStr,10);
    }
    if(y==0)
    	return null;
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}



